/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/util/ArrayUtils.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: ArrayUtils.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.commons.util;

/**
 * @author elancom
 * @date Apr 16, 2012
 */
public abstract class ArrayUtils {
	public static <T> boolean isInArray(T n, T[] array) {
		if (array == null || array.length == 0) {
			return false;
		}

		for (final T t : array) {
			if (t == n) {
				return true;
			}
		}

		return false;
	}

	public static boolean isInArray(int n, int[] array) {
		if (array == null || array.length == 0) {
			return false;
		}

		final int len = array.length;
		for (int i = 0; i < len; i++) {
			if (n == array[i]) {
				return true;
			}
		}
		return false;
	}

	public static boolean isInArray(String s, String[] array) {
		if (array == null || array.length == 0) {
			return false;
		}

		final int len = array.length;
		for (int i = 0; i < len; i++) {
			if (s == array[i] || (array[i] != null && array[i].equals(s))) {
				return true;
			}
		}
		return false;
	}

	public static <T> String join(T[] tArr) {
		if (tArr == null || tArr.length == 0) {
			return "";
		}

		final StringBuffer sb = new StringBuffer();
		for (final T t : tArr) {
			sb.append(t.toString());
		}

		return sb.toString();
	}

	public static String[] toStringArray(Object[] objArr) {
		String[] strArr = null;
		if (objArr != null) {
			strArr = new String[objArr.length];

			Object o = null;
			for (int i = 0; i < objArr.length; i++) {
				o = objArr[i];
				strArr[i] = o.toString();
			}
		} else {
			strArr = new String[0];
		}

		return strArr;
	}

	public static Object[] reverse(Object[] arr, boolean isReturnNewObjectArr) {
		if (arr == null) {
			return isReturnNewObjectArr ? new Object[0] : arr;
		}

		if (arr.length == 1) {
			return isReturnNewObjectArr ? new Object[] { arr[0] } : arr;
		}

		final Object[] returnObj = isReturnNewObjectArr ? new Object[arr.length] : arr;
		Object temp = null;
		for (int start = 0, end = returnObj.length - 1; start < end; start++, end--) {
			temp = returnObj[start];
			returnObj[start] = returnObj[end];
			returnObj[end] = temp;
		}
		return returnObj;
	}

	public static Object[] reverse(Object[] arr) {
		return reverse(arr, false);
	}

	public static void main(String[] args) {
		final String[] s1 = { "1", "2", "3" };
		reverse(s1);
		System.out.println(s1[0] + s1[1] + s1[2]);
	}
}
